// 1.终端输入命令npm install -g nodemon、npm i express和npm i cors安装模块


// 2.后端模块服务内容
const express = require('express');
const app = express();
app.listen(9000, () => {
  console.log('服务器9000启动成功');
});

const cors = require('cors');
app.use(cors());
app.use(express.json());
app.use('/nodejs_server/back_end_static', express.static('./static'));

app.get('/nodejs_server/back_end_get/:params_name', (req, res) => {
  // setTimeout模拟服务端耗时
  setTimeout(() => {
    const data = {
      params: req.params,
      query: req.query,
    };
    
    res.send({
      code: 200,
      msg: '请求成功',
      data: data
    });
  }, 3000);
});

app.post('/nodejs_server/back_end_post/:params_name', (req, res) => {
  // setTimeout模拟服务端耗时
  setTimeout(() => {
    const data = {
      params: req.params,
      body: req.body,
    };
    
    res.send({
      code: 200,
      msg: '请求成功',
      data: data
    });
  }, 3000);
});

app.get('/nodejs_server/back_end_error', (req, res) => {
  // setTimeout模拟服务端耗时
  setTimeout(() => {
    throw new Error('服务器错误');
  }, 3000);
});


// 3.终端输入命令nodemon path启动后端模块服务